const { Model, DataTypes } = require("sequelize");

class Cabinet extends Model {
  static init(sequelize) {
    super.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true,
          comment: "主键，自增",
        },
        number: {
          type: DataTypes.INTEGER,
          allowNull: false,
          comment: "编号",
        },
        belong: {
          type: DataTypes.STRING(60),
          comment: "所属柜组",
        },
        belongid: {
          type: DataTypes.INTEGER,
          comment: "所属柜组ID",
        },
        des: {
          type: DataTypes.STRING(100),
          comment: "描述",
        },
        state: {
          type: DataTypes.STRING(20),
          allowNull: false,
          defaultValue: "0",
          comment: "状态（1为占用，0为空闲，2为维护，默认为0）",
        },
        isclosed: {
          type: DataTypes.STRING(20),
          allowNull: false,
          defaultValue: "1",
          comment: "门是否闭合（1为闭合，0为开启，默认为1）",
        },
        price: {
          type: DataTypes.INTEGER,
          allowNull: false,
          defaultValue: 1,
          comment: "费率（price元/天，默认为1）",
        },
        usedtime: {
          type: DataTypes.DATE,
          comment: "被使用的开始时间",
        },
        usedaccount: {
          type: DataTypes.STRING(20),
          comment: "使用者账号",
        },
        quCode: {
          type: DataTypes.STRING(10),
          allowNull: false,
          defaultValue: "000000",
          comment: "六位取件码",
        },
        createdAt: {
          type: DataTypes.DATE,
          allowNull: false,
          comment: "创建时间",
        },
        updatedAt: {
          type: DataTypes.DATE,
          allowNull: false,
          comment: "更新时间",
        },
      },
      {
        sequelize,
        modelName: "Cabinet",
        tableName: "cabinet",
        timestamps: false, // 禁用自动管理 createdAt 和 updatedAt
        comment: "智能快递柜表",
      }
    );
  }

  static associate(models) {
    // 定义关联关系
    this.belongsTo(models.Users, {
      foreignKey: "usedaccount", // 外键字段
      targetKey: "account", // 关联 Users 表的 account 字段
      as: "user", // 关联别名
    });
  }
}

module.exports = Cabinet;
